#include <header.h>
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd < 0) {
        perror("socket");
        return EXIT_FAILURE;
    }

    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(1280);
    //serveraddr.sin_addr.s_addr = inet_addr("192.168.30.129");
    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");//代表本机地址

    int ret = connect(clientfd, (const struct sockaddr*)&serveraddr, sizeof(serveraddr));
    if(ret < 0) {
        perror("connect");
        close(clientfd);
        return EXIT_FAILURE;
    }

    printf("connect success\n");


    sleep(1);
    const char * msg = "hello, server";
    ret = send(clientfd, msg, strlen(msg), 0);
    printf("send %d bytes.\n", ret);

    //指定用户态接收缓冲区
    char buff[1024] = {0};
    //当第三个参数对数组使用sizeof运算符时，
    //表示最大可以接收数组的空间的数据量
    ret = recv(clientfd, buff, sizeof(buff), 0);
    printf("recv %d bytes.\n", ret);
    printf("content: %s\n", buff);

    //断开连接
    close(clientfd);
    return 0;
}




#if 0

int main()
{
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd < 0)
    {
        cout << "socket" << endl;
        return EXIT_FAILURE;
    }

    struct sockaddr_in serveraddr;
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(1280);
    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int ret = connect(clientfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
    if(ret < 0)
    {
        cout << "Connect error" << endl;
        close(clientfd);//为什么要关闭socket?
                        //因为连接失败了，socket没有用处了
                        // 所以需要关闭它
        return EXIT_FAILURE;
    }

    printf("Connect success\n");

    fd_set readset;
    FD_ZERO(&readset);
    char buf[1024];
    while(1)
    {
        FD_ZERO(&readset);//每次循环都要清空
        FD_SET(clientfd,&readset);//监听服务器端的socket
        FD_SET(STDIN_FILENO,&readset);//监听标准输入,就是从键盘输入数据

        int nready = select(clientfd + 1, &readset, NULL, NULL, NULL);//select函数有什么作用？
                                                                      //select函数监听clientfd和标准输入的事件
                                                                      //如果有事件发生，select函数就会返回
                                                                      //如果没有事件发生，select函数会阻塞，直到有事件发生
                                                                      //如果有事件发生，select函数会返回，nready就是发生事件的数量
        printf("Select nready : %d\n", nready);
        if(FD_ISSET(STDIN_FILENO, &readset))
        {
            //如果标准输入有数据，就读取数据
            memset(buf, 0, sizeof(buf));
            ssize_t temp = read(STDIN_FILENO, buf, sizeof(buf) - 1);
            
            printf("Read  %d\n", temp);
            temp = send(clientfd, buf, temp, 0); //发送到服务器
            printf("Senf  %d\n", temp);
        }

        if(FD_ISSET(clientfd, &readset))
        {
            //如果服务器端有数据，就读取数据
            memset(buf, 0, sizeof(buf));
            ssize_t temp = recv(clientfd, buf, sizeof(buf) - 1, 0);
            if(temp <= 0)
            {
                cout << "Server closed" << endl;
                break;
            }
            printf("Recv %s\n", buf);
        }
    }
    close(clientfd);
    return 0;
}

#endif